import { defineStore } from 'pinia';
import { ref, watch } from 'vue';
import axios from 'axios';

export const useStore = defineStore('data', () => {
    // 根据IP地址获取所在城市信息
    const cityAdcode = ref('');
    const cityName = ref('');
    const inputValue = ref('');
    const center = ref([]);
    const drawType = ref('');
    const addIcon = ref('');
    const zoom = ref(0);

    // 根据ip地址查询城市信息
    const axiosData = async () => {
        const res = await axios.get('https://restapi.amap.com/v3/ip', {
            params: {
                key: '981114a7f1d2dbdc051e2236248e7dab',
            },
        });
        cityAdcode.value = res.data.adcode;
        cityName.value = res.data.city;
        return { cityAdcode, cityName };
    };
    // 根据城市编码获取地图数据
    const mapJson = async () => {
        const res = await axios.get(
            `https://geo.datav.aliyun.com/areas_v3/bound/${cityAdcode.value}.json`
        );
        center.value = res.data.features[0].properties.center;
        return center;
    };
    return {
        cityAdcode,
        cityName,
        inputValue,
        center,
        zoom,
        drawType,
        addIcon,
        axiosData,
        mapJson,
    };
});
